package net.hn.hnms.biz.quality.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.quality.domain.CoalQuality;
import net.hn.hnms.biz.quality.mapper.CommonMapper;
import net.hn.hnms.biz.quality.request.CoalQualityReq;
import net.hn.hnms.biz.quality.service.ICoalQualityService;
import net.hn.hnms.biz.quality.vo.CoalQualityVO;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.web.controller.BaseController;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 煤质信息
 *
 * @author zhaoyang
 * @date 2022-08-01
 */

@Api(value = "煤质信息模块", tags = {"煤质信息"})
@RestController
@RequestMapping("/info")
public class CoalQualityController extends BaseController {


    @Autowired
    private ICoalQualityService coalQualityService;

    @Autowired
    private CommonMapper commonMapper;

    /**
     * 新增煤质信息
     */
    @ApiOperation("新增煤质信息")
    @SaCheckPermission("meizhi:info:add")
    @PostMapping("/add")
    @Log(title = "新增煤质信息", businessType = BusinessType.INSERT)
    public R<Void> add(@Validated @RequestBody CoalQualityReq CoalQuality) {
        return toAjax(coalQualityService.insert(CoalQuality));
    }

    /**
     * 删除煤质信息
     */
    @ApiOperation("删除煤质信息")
    @SaCheckPermission("meizhi:info:delete")
    @ApiImplicitParam(name = "idList", value = "要删除的id值,用逗号间隔", required = true, allowMultiple = true, dataType = "String", paramType = "query")
    @DeleteMapping("delete")
    @Log(title = "删除煤质信息", businessType = BusinessType.DELETE)
    public R<Void> delete(@RequestParam("idList") List<String> idList) {
        coalQualityService.deleteBatch(idList);
        return R.ok();
    }

    /**
     * 修改煤质信息
     */
    @ApiOperation("修改煤质信息")
    @SaCheckPermission("meizhi:info:update")
    @PutMapping("/update")
    @Log(title = "修改煤质信息", businessType = BusinessType.UPDATE)
    public R<Void> update(@Validated @RequestBody CoalQualityReq CoalQuality) {
        coalQualityService.updateById(CoalQuality);
        return R.ok();
    }

    /**
     * 启用/禁用
     */
    @ApiOperation("启用/禁用")
    @SaCheckPermission("meizhi:info:status")
    @PutMapping("/status/change/{id}/{status}")
    public R<Void> changeStatus(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        coalQualityService.changeStatus(id, status);
        return R.ok();
    }

    /**
     * 获取煤质信息列表
     */
    @ApiOperation("获取煤质信息分页列表")
//    @SaCheckPermission("plan:development:page")
    @GetMapping("/page")
    @AutoSetMineCode
    public R<ResponsePage<CoalQualityVO>> page(CoalQualityReq CoalQuality, PageQuery pageQuery) {
        return R.ok(coalQualityService.selectPageList(CoalQuality, pageQuery));
    }

    /**
     * 获取煤质信息列表
     */
    @ApiOperation("获取煤质信息列表")
//    @SaCheckPermission("plan:development:list")
    @GetMapping("/list")
    @AutoSetMineCode
    public R<List<CoalQualityVO>> list(CoalQualityReq CoalQuality) {
        return R.ok(coalQualityService.selectList(CoalQuality));
    }

    /**
     * 获取煤质信息详情
     */
    @ApiOperation("获取煤质信息详情")
//    @SaCheckPermission("plan:development:detail")
    @GetMapping("/{id}")
    public R<CoalQualityVO> detail(@PathVariable("id") String id) {
        return R.ok(coalQualityService.selectDetailById(id));
    }


    @ApiOperation("煤质分析导入")
    @SaCheckPermission("meizhi:info:import")
    @PostMapping("/import")
    @Log(title = "煤质分析导入", businessType = BusinessType.IMPORT)
    public R<Void> principalImport(@RequestParam(name = "date")Long date, @RequestPart MultipartFile file)  {
        coalQualityService.importExcel(date,file,commonMapper);
        return R.ok();
    }

    @GetMapping("/down")
    @SaCheckPermission("meizhi:info:down")
    @ApiOperation("煤质分析导入模板下载")
    @Log(title = "煤质分析导入模板下载", businessType = BusinessType.EXPORT)
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("煤质分析导入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), CoalQuality.class).sheet("煤质分析导入模板").doWrite(ListUtils.newArrayList());
    }


    @GetMapping("/coalAnalysisMonthlyRollup")
    @ApiOperation("煤质分析月加权平均")
    public R<List<CoalQualityVO>> getCoalAnalysisMonthlyRollup(String year) {
        return R.ok(coalQualityService.getCoalAnalysisMonthlyRollup(year));
    }


    @GetMapping("/coalAnalysisYearRollup")
    @ApiOperation("煤质分析年加权平均")
    public R<List<CoalQualityVO>> getCoalAnalysisYearRollup(String year) {
        return R.ok(coalQualityService.getCoalAnalysisYearRollup(year));
    }


}
